Convert a string to Integer in C# without library function
58
Convert a string to Integer in C# without library function -
public static void Main() {
string str = Console.ReadLine();
int j = 0;
int myNumber = 0;
string strReverse =String.Empty;
//Reverse the string
foreach (char temp in str)
{
strReverse = temp + strReverse;
}
foreach (char temp in strReverse)
{
int i = temp - 48; //Ascii character
myNumber = myNumber + i * myPower(10,j);
j++;
}
Console.WriteLine(myNumber);
Console.ReadLine();
}
public static int myPower(int i, int j)
{
int final = 1;
for (int loop =0 ; loop < j; loop++)
final = final * i;
return final;
}